package com.qf.springmvc.controller;

import com.qf.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("data")
@SessionAttributes({"name","age"})
public class DataController {

    @RequestMapping("test1")
    public String test1(HttpServletRequest request, HttpSession session){
        // 到request域中存数据
        request.setAttribute("name", "罗艺鑫");
        request.setAttribute("age", 18);
        // session存数据
        session.setAttribute("data", "2022");
        return "welcome";
    }

    //spring推荐的传值方式
    @RequestMapping("test2")
    public String test2(Model model){
        // model中的数据是放到request中的
        model.addAttribute("name", "李宇森");
        model.addAttribute("age", 17);
        // 如果没有指定key的话，key默认就是value值的类型
        model.addAttribute("大壮");
        User user = new User();
        user.setName("易凯");
        user.setId(11);
        model.addAttribute(user);
        return "welcome";
    }

    @RequestMapping("test3")
    public ModelAndView test3() {
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("name","芳芳");
        return modelAndView;
    }
}
